package com.example.administrator.basedemos.edu;

import com.example.administrator.basedemos.utils.LogUtil;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 创建者     ZCL
 * 创建时间   2018/3/20 17:03
 * 描述	      ${自定义callback }
 * <p>
 * 更新者     $Author$
 * 更新时间   $Date$
 * 更新描述   ${TODO}
 */

public abstract class MyBaseCallBack<T> implements Callback<T> {
    private String TAG ="MyBaseCallBack";

    public void onResponse(Call<T> call, Response<T> response){
        if(response.isSuccessful()){
            doSuccess(response.body());
            }else{
            doError(response.message(),response.errorBody(),response.code());
        }
    }

    private void doError(String message, ResponseBody responseBody, int code) {
        LogUtil.util(TAG,"errorCode:"+code+",message:"+message+",responseBody"+responseBody.toString());
    }

    protected abstract void doSuccess(T body);

    ;

    /**
     * Invoked when a network exception occurred talking to the server or when an unexpected
     * exception occurred creating the request or processing the response.
     */
    public void onFailure(Call<T> call, Throwable t) {
        LogUtil.util(TAG,"NetWork Failed:"+t.getLocalizedMessage()+",服务器开小差了");

    }
}
